﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ARR.DataModel.Repositories;
using ARR.Entities;
using DDF.Entity;
using ARR.Entities.Gateways;
using System.Transactions;

namespace ARR.Services.Domain
{
    public static class CommentsDomain
    {
        //Article Comments
        public static List<Comment> GetAll()
        {
            var rows = CommentsRepository.Select();
            var comments = rows.ConvertAll<Comment>(c => new Comment(c));

            comments.ForEach(c => c.ChangeState(EntityState.Original));

            return comments;
        }

        public static List<Comment> GetByType(string type)
        {
            var rows = CommentsRepository.Select(x => x.Type == type);
            var comments = rows.ConvertAll<Comment>(c => new Comment(c));

            comments.ForEach(c => c.ChangeState(EntityState.Original));

            return comments;
        }

        public static void Save(List<Comment> comments)
        {
            throw new NotImplementedException();
        }


        //General Comments
        public static List<GeneralComment> GetAllGeneralComments()
        {
            var comments = GeneralCommentsRepository.Select().ConvertAll<GeneralComment>(c => new GeneralComment(c));
            var categories = CategoriesRepository.Select().ConvertAll<Category>(x => new Category(x));


            Section section;
            ReportType reportType;
            foreach (var comment in comments)
            {
                if (comment.CategoryId.HasValue)
                {
                    comment.Category = categories.SingleOrDefault(x => x.Id == comment.CategoryId);
                }

                if (Enum.TryParse<Section>(comment.Section, out section))
                {
                    comment.GCSection = section;
                }

                if (Enum.TryParse<ReportType>(comment.ReportType, out reportType))
                {
                    comment.GCReportType = reportType;
                }

                comment.RangeReview = RangeReviewDomain.GetById(comment.RangeReviewId, true);

                comment.ChangeState(EntityState.Original);
            }


            return comments;
        }

        public static void SaveGeneralComments(List<GeneralComment> comments)
        {
            using (TransactionScope scope = new TransactionScope())
            {
                foreach (GeneralComment comment in comments)
                {
                    if (comment.GetEntityState() == EntityState.New)
                    {
                        GeneralCommentsRepository.Insert(comment.GetRow());
                    }
                    else if (comment.GetEntityState() == EntityState.Modified)
                    {
                        GeneralCommentsRepository.Update(comment.GetRow());
                    }
                }

                scope.Complete();
            }
        }
    }
}
